<?php
class Controller_Admin_Resource extends Controller_Template
{
	private $_limit	= 0;
	private	$_resource	= '';
	public function __construct($request, $response)
	{
		$this->_limit	= 20;	
		$this->template = 'smarty:admin/resource/index';
		$this->_resource	= new User_Role_Resource();
		parent::__construct($request, $response);
	}

	public function action_index()
	{
		$function_name  = $this->request->controller();

		$this->template->set('page_title', '添加资源');
		$this->template->set('function_name', $function_name);
		$this->response->body($this->template->render());
	}
	
	public function action_add_resource()
	{
		$name		= $this->request->post('resource_name');	
		$action		= $this->request->post('action');	
		$res_name	= strval($name);

		$flag		= 'no';
		if($res_name && ($action == 'add'))
		{
			$rs	= User_Role_Resource::insert_resources($res_name);
			
			if($rs)
				$flag = 'ok';
		}

		$url	= '/resource/list';

		$this->request->redirect($url , $code = 302);
	}

	public function action_list()
	{
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);

		$data	= $this->_resource->get_resources($offset, $this->_limit);

		$sum	= $this->_resource->count_all_resources();
		
		$page_config	= array(
								'total_items'		=> $sum,
								'items_per_page'	=> $this->_limit,
							);
		$pagination	= Pagination::factory($page_config);
	
		$function_name	= $this->request->controller();
		$this->template->set('function_name', $function_name);
		$this->template->set_filename('smarty:admin/resource/list');
		$this->template->set('data', $data);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('total', $sum);
		$this->template->set('page_title', '资源列表');
		$this->request->body($this->template->render());
	}
}

?>
